using UnityEngine;

namespace NL.Advanced
{
    public class SkillSelector : MonoBehaviour
    {
        //技能选择器类型
        public enum SelectorType
        {
            None = 0,
            Range = 1,  // 范围选择器....某半径的圆
            Box = 2,      // 矩形选择器.... 使用 xy为边长
            Direction = 11, // 方向选择器...
        }

        [Header("技能选择器类型")]
        public SelectorType Type = SelectorType.None;
        public Vector3 Size;

        public bool AutoLockTarget = false;

        public GameObject Mesh;

        public Transform Destney
        {
            get
            {
                if (AutoLockTarget)
                {
                    if (TargetCollector != null)
                    {
                        var topone = TargetCollector.GetTopOne();
                        if (topone != null && topone.transform != null)
                            return topone.transform;
                    }
                }

                return transform;
            }
        }

        public TargetCollecter TargetCollector { get; private set; }

        private Material _DisplayMaterial;

        // Start is called before the first frame update
        void Start()
        {
            TargetCollector = GetComponentInChildren<TargetCollecter>();

            var mesh = GetComponent<MeshRenderer>();
            if (mesh)
            {
                _DisplayMaterial = mesh.material;
            }
        }

        //public SkillSelector Set(SkillSelectorType type)
        //{
        //    Type = type;return this;
        //}

        public SkillSelector SetOwner(ICombatOwner combatOwner)
        {
            if (TargetCollector != null)
            {
                TargetCollector.SetOwner(combatOwner);
            }

            return this;
        }

        public SkillSelector SetSize(float size)
        {
            Size.x = size; _ReSize(); return this;
        }
        public SkillSelector SetSize(float width, float height)
        {
            Size.x = width; Size.y = height; _ReSize(); return this;
        }
        public SkillSelector SetSize(Vector3 size)
        {
            Size = size;
            _ReSize();
            return this;
        }
        Vector3 WorldPos;
        public SkillSelector Repos(Vector3 pos)
        {
            WorldPos = pos;
            return this;
        }
        public SkillSelector ReForword(Vector3 dir)
        {
            dir.y = 0;
            transform.forward = dir.normalized;



            return this;
        }
        void _ReSize()
        {
            if (Mesh != null)
            {
                switch (Type)
                {
                    case SelectorType.None:

                        Mesh.transform.localScale = new Vector3(Size.x, 1, Size.x); break;
                    case SelectorType.Range:

                        Mesh.transform.localScale = new Vector3(Size.x, 1, Size.x); break;
                    case SelectorType.Box:
                        Mesh.transform.localScale = new Vector3(Size.x, 1, Size.y); break;

                    case SelectorType.Direction:
                        Mesh.transform.localScale = new Vector3(1, 1, Size.x); break;
                    default:
                        break;
                }
            }
        }
        public SkillSelector Show(bool show)
        {
            if (Mesh != null)
            {
                if (show) _ReSize();
                Mesh.SetActive(show);
            }
            return this;
        }

        public void SetRenderQueue(int renderQueue)
        {
            if (!_DisplayMaterial)
            {
                return;
            }

            _DisplayMaterial.renderQueue = renderQueue;
        }

        private void LateUpdate()
        {
            transform.position = WorldPos;
            transform.rotation = Quaternion.Euler(0f, 0f, 0f);
        }
    }
}

